{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "view-in-github"
   },
   "source": [
    "<a href=\"https://colab.research.google.com/github/Dene33/mlcourse.ai/blob/master/jupyter_english/tutorials/Useful_Google_Colab_snippets.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "qgq9XRjSYknq"
   },
   "source": [
    "For now I mainly use Google Colab for my ML researches. I love the way of having machine with Linux and GPU in browser for free. I can start it without messing with virtual environments, restart it in few clicks if I did something wrong, upload and download files, etc. I find it really flexible.\n",
    "\n",
    "But sometimes that's not so easy, for example, to replicate someone's implementation of Neural-Networks. That's because of dependencies that are not installed and lack of terminal in Google Colab.\n",
    "\n",
    "I've gathered few useful (at least for myself) snippets of code for Google Colab that could save a lot of time if you're trying to run some Neural-Network implementations from github (2d and 3d pose estimations in my case)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "3uieS9GNxhjt"
   },
   "source": [
    "##Install Python 3.5 (or any other version of your choice)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "Dgp8iT06yxGC"
   },
   "source": [
    "The code snippet below will install Python 3.5 without any Colab pre-installed libraries (such as Tensorflow). You can install them later with pip, like `!pip install tensorflow`\n",
    "\n",
    "To run python scripts with 3.5 version, use `!python3.5`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "TEUYMtKsw-u2"
   },
   "outputs": [],
   "source": [
    "!add-apt-repository ppa:deadsnakes/ppa\n",
    "!apt-get update\n",
    "!apt-get install python3.5\n",
    "!apt-get install python3.5-dev\n",
    "\n",
    "!wget https://bootstrap.pypa.io/get-pip.py && python3.5 get-pip.py\n",
    "\n",
    "import sys\n",
    "\n",
    "sys.path[2] = \"/usr/lib/python35.zip\"\n",
    "sys.path[3] = \"/usr/lib/python3.5\"\n",
    "sys.path[4] = \"/usr/lib/python3.5/lib-dynload\"\n",
    "sys.path[5] = \"/usr/local/lib/python3.5/dist-packages\"\n",
    "sys.path[7] = \"/usr/local/lib/python3.5/dist-packages/IPython/extensions\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "75bVaKUxzXg4"
   },
   "source": [
    "##Install Caffe2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "ZTVQnY5v4MZm"
   },
   "source": [
    "Trying a lot of different (not working) approaches of installing Caffe2, I ended up with this one. The code snippet below will install Caffe2.\n",
    "\n",
    "Because Caffe2 runs with Python 2.7, it's better to use Google Colab **Python 2 runtime type**. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "Lo29NqX5zZA_"
   },
   "outputs": [],
   "source": [
    "!wget https://anaconda.org/pytorch/pytorch-nightly/1.0.0.dev20181206/download/linux-64/pytorch-nightly-1.0.0.dev20181206-py2.7_cuda9.2.148_cudnn7.4.1_0.tar.bz2\n",
    "!tar xvjf pytorch-nightly-1.0.0.dev20181206-py2.7_cuda9.2.148_cudnn7.4.1_0.tar.bz2\n",
    "!cp -r lib/python2.7/site-packages/* /usr/local/lib/python2.7/dist-packages/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "sVLAD7L238bq"
   },
   "outputs": [],
   "source": [
    "# To check if Caffe2 build was successful\n",
    "!python2 -c 'from caffe2.python import core' 2>/dev/null && echo \"Success\" || echo \"Failure\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "XjRu4yoD8IG_"
   },
   "source": [
    "##Execute .py file as cell \n",
    "This will be useful if you have a lot of code what just clutters up your notebook. Notice that this snippet will run code exactly like cell (i.e. loading all the variables into botebook, etc), not just like a python script.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "RjL1kqjj9KCN"
   },
   "outputs": [],
   "source": [
    "exec(open(\"PATH_TO_PYTHON_FILE\").read())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "k5xaS3-c-LLa"
   },
   "source": [
    "##Overwrite or create new files within cell"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "eHKKEUYk-kUB"
   },
   "source": [
    "That's a really useful magic command to easily create new files or overwrite existing ones."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "vOTj__Gr-Td0"
   },
   "outputs": [],
   "source": [
    "%%writefile YOUR_FILE.py\n",
    "\n",
    "##Anything you'd like"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "gekgVDVQMVH3"
   },
   "source": [
    "##Better way of mounting Google Drive"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "pH5n5OomMfct"
   },
   "source": [
    "This snippet uses `/content/gdrive` path instead of Colab's default snippet `/gdrive` so you can manage all your files visualy in `Files` menu on the left.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "o5niQbseLJLo"
   },
   "outputs": [],
   "source": [
    "from google.colab import drive\n",
    "\n",
    "drive.mount(\"/content/gdrive\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "5qW7krBs5A08"
   },
   "source": [
    "##Download multiple files from Google Colab"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "lpfqHWWm5FEs"
   },
   "source": [
    "The code snippet below will download multiple files from defined path from Google Colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "Ziuhm-cg5ATv"
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from google.colab import files\n",
    "\n",
    "data_path = \"sample_data\"\n",
    "\n",
    "for file in os.listdir(data_path):\n",
    "    files.download(data_path + \"/\" + file)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "NMjsfAAwT76H"
   },
   "source": [
    "##Install Kaggle-API"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "T-SHisQ5USsW"
   },
   "source": [
    "First, create an API key in Kaggle.\n",
    "\n",
    "To do this, go to [kaggle.com](https://www.kaggle.com) and open your user settings page.\n",
    "\n",
    "![alt text](https://i.stack.imgur.com/jxGQv.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "DaIk5lnWUd4o"
   },
   "source": [
    "Next, scroll down to the API access section and click generate to download an API key. \n",
    "![alt text](https://i.stack.imgur.com/Hzlhp.png)\n",
    "\n",
    "This will download a file called kaggle.json to your computer. You'll use this file in Colab to access Kaggle datasets and competitions.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "o-MUwA6hT-qP"
   },
   "outputs": [],
   "source": [
    "# Run this cell and select the kaggle.json file downloaded\n",
    "# from the Kaggle account settings page.\n",
    "from google.colab import files\n",
    "\n",
    "files.upload()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "sipZJjdhUCr-"
   },
   "outputs": [],
   "source": [
    "# Let's make sure the kaggle.json file is present.\n",
    "!ls -lha kaggle.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "6WQ3rqVUULNN"
   },
   "outputs": [],
   "source": [
    "# Next, install the Kaggle API client.\n",
    "!pip install -q kaggle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "pOJpkvKQUOIa"
   },
   "outputs": [],
   "source": [
    "# The Kaggle API client expects this file to be in ~/.kaggle,\n",
    "# so move it there.\n",
    "!mkdir -p ~/.kaggle\n",
    "!cp kaggle.json ~/.kaggle/\n",
    "\n",
    "# This permissions change avoids a warning on Kaggle tool startup.\n",
    "!chmod 600 ~/.kaggle/kaggle.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "ag3E5_hhURhH"
   },
   "outputs": [],
   "source": [
    "# List available datasets.\n",
    "!kaggle datasets list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "JHYqnsBNVMKc"
   },
   "outputs": [],
   "source": [
    "# Copy the stackoverflow data set locally.\n",
    "!kaggle datasets download -d stackoverflow/stack-overflow-2018-developer-survey"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "SytTUVCvVMl9"
   },
   "outputs": [],
   "source": [
    "!head ~/.kaggle/datasets/stackoverflow/stack-overflow-2018-developer-survey/survey_results_public.csv"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "R42VKK6BAav2"
   },
   "source": [
    "##Processing videos with ffmpeg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "njl5f6fTD222"
   },
   "outputs": [],
   "source": [
    "!apt install ffmpeg"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "9bU0Gu5GDsmI"
   },
   "source": [
    "####Crop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "sU-2oclsAhUp"
   },
   "outputs": [],
   "source": [
    "# out_w is the width of the output rectangle\n",
    "# out_h is the height of the output rectangle\n",
    "# x and y specify the top left corner of the output rectangle\n",
    "!ffmpeg -i INPUT_FILE_NAME.mp4 -filter:v \"crop=out_w:out_h:x:y\" OUTPUT_FILE_NAME.mp4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "sRYTTXRnEAWv"
   },
   "source": [
    "####Trim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "P3UdA9raEAeO"
   },
   "outputs": [],
   "source": [
    "# -ss: Used with -i, this seeks in the input file (input.mp4) to position.\n",
    "# 00:01:00: This is the time your trimmed video will start with.\n",
    "# -to: This specifies duration from start (00:01:40) to end (00:02:12).\n",
    "!ffmpeg -ss 00:01:00 -i INPUT_FILE_NAME.mp4 -to 00:02:00 -c copy OUTPUT_FILE_NAME.mp4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "S9JBO-4TDwMW"
   },
   "source": [
    "####Video to images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "dd-wSJxXDydc"
   },
   "outputs": [],
   "source": [
    "!ffmpeg -i INPUT_FILE_NAME.mpg -r DESIRED_FPS_RATE 'OUTPUT_FILE_NAME%03d.png'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "2mmgB_aSHZXg"
   },
   "source": [
    "####Resize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "WvWo9TpoHbpc"
   },
   "outputs": [],
   "source": [
    "# Fixed width and height\n",
    "!ffmpeg -i INPUT_FILE_NAME.avi -vf scale=\"OUTPUT_WIDTH:OUTPUT_HEIGHT\" OUTPUT_FILE_NAME.avi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "duz3aL7wH7O0"
   },
   "outputs": [],
   "source": [
    "# Retain aspect ratio\n",
    "# resize based on the width\n",
    "!ffmpeg -i INPUT_FILE_NAME.avi -vf scale=\"NEW_WIDTH:-1\" OUTPUT_FILE_NAME.avi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "yTWa_77fIQdG"
   },
   "outputs": [],
   "source": [
    "# Scale based on input\n",
    "!ffmpeg -i INPUT_FILE_NAME.avi -vf scale=\"INPUT_WIDTH/1:INPUT_HEIGHT/2\" OUTPUT_FILE_NAME.avi"
   ]
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "collapsed_sections": [],
   "include_colab_link": true,
   "name": "Useful_Google_Colab_snippets.ipynb",
   "provenance": [],
   "version": "0.3.2"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
